<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <%- include ../taglibs/layout_head.ejs%>
    <%- include ../taglibs/layout.css.ejs%>
    <link href="js/plugins/zTree/css/metroStyle/metroStyle.css" rel="stylesheet">
</head>
<body>
<%-include("../taglibs/header.ejs",{params:{className:"active",systemIsActive:"true"}})%>
<div class="nav-body" id="divContent">
    <div class="nav-box">
        <div class="row">
            <div class="col-sm-4 col-md-3 col-lg-2 nav-box-left-box">
                <%-include("systemLeftMenu.ejs",{params:{className:"active",powerSetting:"true"}})%>
            </div>
            <div class="col-sm-6 col-md-9 col-lg-10 right-container-box">
                <div class="right-container"  style="margin-top: 25px">
                    <section class="content content-search">
                        <div class="row">
                            <div class="col-sm-12">
                                <div class="col-sm-12 col-md-8">
                                    <div class="col-sm-12 col-md-12 content-search-left">
                                        <input type="text" name="carids" id="carids" hidden="hidden">
                                        <input type="text" name="carbaseTerminals" id="carbaseTerminals" hidden="hidden">
                                        <!--<div class="col-sm-12 col-md-6 col-lg-4 content-search-left-box">
                                            <label class="col-sm-4">
                                                开始时间：
                                            </label>
                                            <div class="col-sm-8">
                                                <input type="text" class="form-control   layer-date " id="start" name="start">
                                            </div>
                                        </div>
                                        <div class="col-sm-12 col-md-6 col-lg-4 content-search-left-box">
                                            <label class="col-sm-4">
                                                结束时间：
                                            </label>
                                            <div class="col-sm-8">
                                                <input type="text" class="form-control   layer-date " id="end" name="end">
                                            </div>
                                        </div>-->
                                        <div class="col-sm-12 col-md-6 col-lg-6 content-search-left-box">
                                            <label class="col-sm-4">
                                                选择车辆：
                                            </label>
                                            <div class="col-sm-8">
                                                <input type="text"
                                                       class="form-control" id="caridName" name="caridName" style="padding-right: 54px">
                                                <button type="button" class="btn btn-theme selectCar"
                                                        style="position: absolute;top: 0px;right: 0px;">选择</button>
                                            </div>
                                        </div>
                                        <!--<div class="col-sm-12 col-md-6 col-lg-4 content-search-left-box">-->
                                            <!--<label class="col-sm-4">-->
                                                <!--车牌号：-->
                                            <!--</label>-->
                                            <!--<div class="col-sm-8">-->
                                                <!--<input type="text" class="form-control" id="baseCarNo" name="baseCarNo">-->
                                            <!--</div>-->
                                        <!--</div>-->
                                        <!--<div class="col-sm-12 col-md-6 col-lg-4 content-search-left-box">-->
                                            <!--<label class="col-sm-4">-->
                                                <!--车主姓名：-->
                                            <!--</label>-->
                                            <!--<div class="col-sm-8">-->
                                                <!--<input type="text" class="form-control" id="userName" name="userName">-->
                                            <!--</div>-->
                                        <!--</div>-->
                                    </div>
                                </div>
                                <div class="col-sm-12 col-md-4 " style="text-align: right">
                                    <div class="col-sm-12 col-md-12 " >
                                        <button type="button" class="btn btn-round btn-theme" id="do_search" style="margin-bottom: 3px;margin-top: 3px">查 询</button>
                                        <button type="button" class="btn btn-round btn-danger" id="do_reset" style="margin-bottom: 3px;margin-top: 3px">重 置</button>
                                        <!--<button type="button" class="btn btn-round btn-theme"   onclick="openExport()">导出列表</button>-->
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                    <section class="content content-table">
                        <div style="display: block">
                            <div id="toolbar" class="btn-group">
                                <button id="btn_add" type="button"  class="btn btn-default ">
                                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
                                </button>
                                <button  type="button" class="btn btn-default "  onclick="openImport()">
                                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>导入
                                </button>
                                <button  type="button" class="btn btn-default"  onclick="openExport()">
                                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>导出
                                </button>
                                <!--<button id="btn_delete" type="button" class="btn btn-default" onclick="deleteRows()">-->
                                    <!--<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>批量删除-->
                                <!--</button>-->
                                <!--<button id="btn_delete" type="button" op_resId="14_36" class="btn btn-default dp-none" onclick="deleteRows()">-->
                                    <!--<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>批量删除-->
                                <!--</button>-->
                            </div>
                            <table id="powerTable"></table>
                        </div>
                    </section>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myCarTreeDialog" style="z-index: 100099">
    <div class="modal-dialog" style="width: 320px;" >
        <div class="modal-content" style="width: 320px;">
            <div class="modal-header">
                <button type="button" class="close" onclick="closeTreeDialog()">×</button>
                <h4 class="modal-title" id="NoPermissionModalLabel">车辆</h4>
            </div>
            <div class="modal-body" style="height:320px;max-height:320px;max-width: 320px;
                overflow: auto;">
                <ul id="userCommonTree" class="ztree">
                </ul>
            </div>
            <div class="modal-footer">
                <button class="btn btn-info"   type="button" onclick="getAllCheckNodes()">确 定</button>
            </div>
        </div>
    </div>
</div>
<div class="modal inmodal" id="addPower" >
    <div class="modal-dialog" >
        <div class="modal-content animated bounceInRight">
            <div class="modal-header">
                <button type="button" class="close" onclick="closePowerDialog()">×</button>
                <h4 class="modal-title" id="NoPermissionModalLabel">新增</h4>
            </div>
            <div class="modal-body" style="max-height: 400px; ">
                <form class="form-horizontal m-t" >
                    <input type="hidden" id="selectCarId" name="selectCarId">
                    <input type="hidden" id="carNo" name="carNo">
                    <input type="hidden" id="baseTerminalCode" name="baseTerminalCode">
                    <input type="hidden" id="id" name="id">
                    <div class="form-group" >
                        <label class="col-sm-3 control-label label-require" style="font-size: 14px;">车辆：</label>
                        <div class="col-sm-8">
                            <input type="text"
                                   class="form-control" id="carName" name="carName">
                            <button type="button" class="btn btn-theme selectPowerCar"
                                    style="position: absolute;top: 0px;right: 0px;">选择</button>
                        </div>
                    </div>
                    <div class="form-group " id="ipBox" >
                        <label class="col-sm-3 control-label label-require" style="font-size: 14px;">耗电量：</label>
                        <div class="col-sm-8">
                            <input id="baseTerminalNum" name="baseTerminalNum" class="form-control"
                                    type="text" aria-required="true"
                                   aria-invalid="true">
                        </div>
                    </div>
                    <div class="form-group " id="timeBox" >
                        <label class="col-sm-3 control-label label-require" style="font-size: 14px;">时间：</label>
                        <div class="col-sm-8">
                            <input id="timeName" name="lastUpdateTime" class="form-control"
                                   placeholder="" type="text" aria-required="true"
                                   aria-invalid="true">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" id="closePowerModal" class="btn btn-white" >取消</button>
                <button type="button" id="suerPowerModal" class="btn btn-primary" >确认</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myModalImp">
    <div class="modal-dialog" >
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" onclick="closeImport()">×</button>
                <h4 class="modal-title" id="NoPermissionModalLabel">文件上传</h4>
            </div>
            <div class="modal-body" style="height:320px">
                <iframe id="NoPermissioniframe" width="100%" height="70%" frameborder="0"></iframe>
            </div>
            <!--<div class="modal-footer">-->
            <!--<button class="btn btn-default"  type="button" onclick="closeImport()" >    关  闭    </button>-->
            <!--</div>-->
        </div>
    </div>
</div>
<%-include ../taglibs/layout.js.ejs%>
<script src="/js/plugins/zTree/js/jquery.ztree.all-3.5.js"></script>
<script src="/js/plugins/zTree/js/jquery.ztree.exhide-3.5.js"></script>
<script src="/js/ztree/ztree-cus-check.js"></script>
<script src="/js/ztree/ztree-user.js"></script>
<!--时间-->
<script src="js/plugins/layer/laydate/laydate.js"></script>
<script type="text/javascript" src="js/viewpage/systemManage/systemManage.js"></script>
<script type="text/javascript">
    var isAdd=false;
    var isInit = true
    var dataTable;
    $(function () {
        dataTable = new BootstrapTable($('#powerTable'),{
            url: '/powerSetting/queryPowerList',
            sidePagination: "server",
            height:500,
            toolbar: '#toolbar',
            singleSelect:false,
            queryParams:function (params) {
                var _searchParams = {
                    terminalId:$("#carbaseTerminals").val()
                }
                return $.extend(params,_searchParams);
            },
            columns: [{
                field: 'carNo',
                title: '车牌号'
            }, {
                field: 'baseTerminalCode',
                title: '设备编号'
            }, {
                field: 'baseTerminalNum',
                title: '耗电量'
            },{
                field: 'lastUpdateTime',
                title: '时间'
            },
                {
                    field: 'id',
                    title: '操作',
                    formatter:function (value,row,index) {
                        var opHtml = "";
//                        var isEdit = AuthUtils.getOpAuthById("14_35");
//                        var isDel = AuthUtils.getOpAuthById("14_36");
//                        if(isEdit){
//                            opHtml+= "<button type=\"button\" class=\"btn  btn-theme\" style=\"margin-bottom: 3px;margin-top: 3px;margin-right: 3px;padding: 3px 6px;\" onclick='editUserView("+row.id+")'>修 改</button>";
//                        }
//                        if(isDel){
//                            opHtml+= "<button type=\"button\" class=\"btn btn-danger\"  style=\"margin-bottom: 3px;margin-top: 3px;padding: 3px 6px;\" onclick='deleteRow("+row.id+")'>删 除</button>";
//                        }
                        opHtml+= "<button type=\"button\" class=\"btn  btn-theme\" style=\"margin-bottom: 3px;margin-top: 3px;margin-right: 3px;padding: 3px 6px;\" onclick='editPower("+JSON.stringify(row)+")'>修 改</button>";
                        opHtml+= "<button type=\"button\" class=\"btn btn-danger\"  style=\"margin-bottom: 3px;margin-top: 3px;padding: 3px 6px;\" onclick='deletePowerRow("+row.id+")'>删 除</button>";
                        return opHtml
                    }
                }
            ]
        });
        dataTable.init();
        var start = {
            elem: '#timeName',
            format: 'YYYY-MM-DD hh:mm:ss',
//            min: laydate.now(), //设定最小日期为当前日期
            max: '2999-06-16 23:59:59', //最大日期
//            start: laydate.now(),
//            start: laydate.now() + ' 00:00:00',
            istime: true,
            istoday: false,
            festival: true, //是否显示节日
            choose: function (datas) {
                if(datas){
                    end.min =datas.substring(0,10)+" 00:00:00"
                    end.start = datas.substring(0,10)+" 00:00:00"
                }
//                end.min = datas; //开始日选好后，重置结束日的最小日期
//                end.start = datas //将结束日的初始值设定为开始日
            }
//            min: laydate.now(-1), //-1代表昨天，-2代表前天，以此类推
//            max: laydate.now(+1) //+1代表明天，+2代表后天，以此类推
        };
//        var end = {
//            elem: '#end',
//            format: 'YYYY-MM-DD hh:mm:ss',
////            min: laydate.now(),
//            max: '2999-06-16 23:59:59',
////            start: laydate.now() + ' 23:59:59',
//            istime: true,
//            istoday: false,
//            festival: true, //是否显示节日
//            choose: function (datas) {
//                var st=$("#start").val();
//                if(st>datas){
//                    swal({
//                        title: "系统提示",
//                        text: "开始时间必须小于结束时间!",
//                        type: "error"
//                    });
////                    $("#end").val(laydate.now() + ' 23:59:59');
//                }else{
//                    start.max = datas.substring(0,10)+" 23:59:59"; //结束日选好后，重置开始日的最大日期
//                }
//            }
//        };
        laydate(start);
//        laydate(end);
        $("#timeName").val(laydate.now(0, 'YYYY-MM-DD hh:mm:ss'));
//        $("#end").val(laydate.now() + ' 23:59:59');
        var urlTree = '/tree/deptCar';
        initCartree('id', 'parentId', 'name',  fnClick,fnCheck, "radio");
        loadMyCarTree(urlTree,"userCommonTree",true);
        $(".selectCar").on("click",function () {
            isAdd = false;
            $("#myCarTreeDialog").modal("show");
            var id = $("#carids").val();
            if(tree_Obj){
                var nodes = tree_Obj.getCheckedNodes();
                for (var i=0, l=nodes.length; i < l; i++) {
                    if(nodes[i].id != id){
                        tree_Obj.checkNode(nodes[i], false);
                    }
                }
            }

        });
        $(".selectPowerCar").on("click",function () {
            isAdd = true;
            $("#myCarTreeDialog").modal("show");
            var _name = $("#carNo").val();
            if(tree_Obj){
                var nodes = tree_Obj.getCheckedNodes();
                for (var i=0, l=nodes.length; i < l; i++) {
                    if(nodes[i].name != _name){
                        tree_Obj.checkNode(nodes[i], false);
                    }
                }
            }
        })
        $("#do_search").on("click",function () {
            var _searchParams = {
//                start:$("#start").val(),
//                end:$("#end").val(),
//                isSearch:true
            }
//            if($("#carids").val()==""){
//                return $.messageExt.alert("系统提示","请选择车辆","warning");
//            }
//            if($("#carids").val()){
//                _searchParams["carIds"] =$("#carids").val();
//            }else{
//                _searchParams["baseCarNo"] =$("#baseCarNo").val();
//            }
//            _searchParams["userName"] = $("#userName").val()
            _searchParams["terminalId"] = $("#carbaseTerminals").val();
            dataTable.refresh({
                query:_searchParams
            });
            //queryChart()
        });
        $("#do_reset").on("click",function () {
//            $("#start").val("");
//            $("#end").val("");
            $("#carids").val("");
//            $("#baseCarNo").val("")
            $("#caridName").val("");
            $("#carbaseTerminals").val("");
        });
        $("#btn_add").click(function () {
            clearText();
            $("#NoPermissionModalLabel").text("新增");
            $("#carName").prop("disabled",false);
            $(".selectPowerCar").show();
              $("#addPower").modal("show");
        });
        $("#closePowerModal").click(function () {
            closePowerDialog()
        });
        $("#suerPowerModal").click(function () {
            savePower();
        })

    });
    function closeTreeDialog() {
        $("#myCarTreeDialog").modal("hide");
    }
    function closePowerDialog() {
        $("#addPower").modal("hide");
        clearText();
    }
    function clearText() {
        $("#id").val("");
        $("#carNo").val("");
        $("#baseTerminalCode").val("");
        $("#carName").val("");
        $("#selectCarId").val("");
    }
    function savePower() {
        if($("#carNo").val() == ""){
            return $.messageExt.alert("系统提示","请先选择车辆！","warning");
        }
        if($("#baseTerminalNum").val() == ""){
            return $.messageExt.alert("系统提示","请填写耗电量！","warning");
        }else if(isNaN($("#baseTerminalNum").val())){
            return $.messageExt.alert("系统提示","耗电量请填写数字！","warning");
        }

        var data = {
            carNo:$("#carNo").val(),
            baseTerminalCode:$("#baseTerminalCode").val(),
            baseTerminalNum:$("#baseTerminalNum").val(),
            lastUpdateTime:$("#timeName").val()
        };
        if($("#id").val()){
            data["id"] = $("#id").val();
            $.post("/powerSetting/saveData",data,function (result) {
                if(result.isSuccess){
                    $.messageExt.alert("系统提示","修改成功","success");
                    closePowerDialog();
                    dataTable.refresh();
                }else{
                    $.messageExt.alert("系统提示",result.message ? result.message : "修改失败","success")
                }
            },"json")
        }else{
            $.post("/powerSetting/saveData",data,function (result) {
                if(result.isSuccess){
                    $.messageExt.alert("系统提示","保存成功","success");
                    closePowerDialog();
                    dataTable.refresh();
                }else{
                    $.messageExt.alert("系统提示",result.message ? result.message : "保存失败","success")
                }
            },"json")
        }

    }
    
    function editPower(row) {
        $("#NoPermissionModalLabel").text("修改");
        $("#carName").prop("disabled","disabled");
        $(".selectPowerCar").hide();
        $("#addPower").modal("show");
        var data = row;
        if(data){
            for(var k in data){
                if(data.hasOwnProperty(k)){
                    $("#"+k).val(data[k]);
                }
            }
            $("#carName").val(data["carNo"])
        }
    }
    
    function deletePowerRow(id) {
        $.ajax({
            type: "POST",
            url: "/powerSetting/deleteUser",
            async: true,
            dataType: "json",
            data:{ids:id},
            success: function (data) {
                $.messageExt.alert("系统提示","删除成功！","success");
                dataTable.refresh();
            }
        });
    }
    /**
     *
     * @param id 当前节点id
     * @param pid 父节点id
     * @param name 显示名称
     */
    function initCartree(id,pid,name,fnClick,fnCheck,chkStyle){
        _idKey=id;
        _pIdKey=pid;
        _name=name;
        // _clickTree=flag;
        if(fnClick){
            _fnClick=fnClick;
        }
        if(fnCheck){
            _fnCheck=fnCheck;
        }
        var check={
            enable: false,
            // chkboxType: { "Y": "p", "N": "s" }
        };
        if(chkStyle){
            check={
                enable: true,
                chkStyle: chkStyle,
                // chkboxType: { "Y": "p", "N": "s" }
            };
        }
        setting = {
            view: {
                fontCss: getFontCss,
                selectedMulti: false,
                expandSpeed: "fast"
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: _idKey,
                    pIdKey: _pIdKey,
                    rootId: ""
                },
                key: {
                    name: _name
                }
            },
            check: check,
            callback: {
                onClick: _fnClick,
                onCheck: _fnCheck
            }
        }
    }
    function loadMyCarTree(url,treeid,asyncKey) {
        var ztreeId="kindTree";
        if(treeid){
            ztreeId=treeid;
        }
        $.ajax({
            async:asyncKey ? asyncKey : false,
            url: url,
            type: "post",
            dataType: 'json',
            success: function (data) {
                treeNodes_obj = data;
                for (var key in treeNodes_obj) {
                    treeNodes_obj[key].open = true;
                }
                zTree_obj = $.fn.zTree.init($("#"+ztreeId), setting, treeNodes_obj);
                tree_Obj = $.fn.zTree.getZTreeObj(ztreeId);

            }
        })
    }

    var fnCheck = function (event, treeId, treeNode) {
        //selectTreeNode(treeNode);
    }
    //    树节点选中
    var fnClick = function (event, treeId, treeNode) {
        selectTreeNode(treeNode);
    }


    //    复选框选中
    function selectTreeNode(treeNode) {
        var isCar = treeNode.isCar
        if (isCar) {
            $("#h_c_car_id").val(treeNode.id);
        }else{
            swal("系统提示", "请选择车辆查询!", "warning");

        }
    }
    function getAllCheckNodes() {
        var nodes = zTree_obj.getCheckedNodes(true);
        var nodeIds = [];
        var nodeNames = [];
        var nodeTerminal = [];
        var nodeCarNos = [];
        var nodeTerminalCodes = [];
        for(var n = 0,_len=nodes.length;n<_len;n++){
            var _node =nodes[n];
            if(_node.id!=-99999 && !_node.isParent){
                nodeIds.push(_node.id);
                nodeNames.push(_node.name);
                nodeTerminal.push(_node.baseTerminalId);
            }
        }
        if(nodeIds.length){
            if(isAdd){
                $("#carNo").val(nodeNames.toString());
                $("#baseTerminalCode").val(nodeTerminal.toString());
                $("#carName").val(nodeNames.toString());
                $("#selectCarId").val(nodeIds.toString());
            }else{
                $("#caridName").val(nodeNames.toString());
                $("#carids").val(nodeIds.toString());
                $("#carbaseTerminals").val(nodeTerminal.toString());
            }

        }else{
            $("#caridName").val("");
            $("#carids").val("");
            $("#carbaseTerminals").val("");
        }
        closeTreeDialog();
    }

    function openExport() {
        window.location.href = window.serveraddr+"/busTotalPower/exportExcelOutLine?terminalId="+$("#carbaseTerminals").val();
    }
    function openImport() {
        var frameSrc = "<%= serverurl%>/baseCarExcel/indexPower";
        $("#NoPermissioniframe").attr("src", frameSrc);
//        $('#NoPermissionModal').modal({ show: true, backdrop: 'static' });
        $("#myModalImp").modal("show");
    }
    function closeImport() {
        $("#myModalImp").modal("hide");
    }
</script>
</body>
</html>